package cn.kent.middle;


/**
 * 617. 合并二叉树
 *
 * @desc hot100
 * @author Kent
 * @date 2022/8/8 14:00
 */
public class MergeTrees {
    public static void main(String[] args) {
        TreeNode head_a = new TreeNode(0);
        TreeNode head_l = new TreeNode(1);
        TreeNode head_r = new TreeNode(2);
        head_a.left = head_l;
        head_a.right = head_r;
        TreeNode head_ll = new TreeNode(3);
        TreeNode head_rr = new TreeNode(4);
        TreeNode head_lll = new TreeNode(5);
        head_l.left = head_ll;
        head_ll.left = head_rr;
        head_rr.left = head_lll;

        TreeNode head_b = new TreeNode(3);
        TreeNode head_bl = new TreeNode(1);
        TreeNode head_br = new TreeNode(2);
        head_b.left = head_bl;
        head_b.right = head_br;

        TreeNode res = mergeTrees(head_a, head_b);
        System.out.println(res);

    }


    /**
     *
     */
    public static TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null)
            return root2;
        if (root2 == null)
            return root1;
        TreeNode merged  = new TreeNode(root1.val + root2.val);
        merged.left= mergeTrees(root1.left, root2.left);
        merged.right = mergeTrees(root1.right, root2.right);
        return merged;
    }

    /**
     * 先序遍历
     */
    private static TreeNode cycleTreeNode(TreeNode root1) {
        if (root1 != null) {
            System.out.println(root1.val);

            if (root1.left != null) {
                cycleTreeNode(root1.left);
            }

            if (root1.right != null) {
                cycleTreeNode(root1.right);
            }

        }
        return null;
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
